<?php
/**
 * This file contains the Session handler class
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: session.class.php 79 2010-06-23 14:11:12Z sruttloff $
 * @link      phpmy
 */

/**
 * Session handler and login routine for the CMS
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class session
{
    /**
     * @var int user id
     */
    var $user_id;
    /**
     * @var object instance
     */
    protected static $INSTANCE;
    /**
     * @var string user first name
     */
    var $user_firstname;
    /**
     * @var string user last name
     */
    var $user_surname;
    /**
     * @var string user email
     */
    var $user_email;
    /**
     * @var string user role(s)
     */
    var $user_role;
    /**
     * @var string session identifier
     */
    var $SESSIONNAME = "PHPSESSION";
    /**
     * @var		string	path where to save session datas
     */
    var $SESSIONPATH = null;
    /**
     * @var integer default cookie lifetime
     */
    protected $session_cookielifetime = 20;
    /**
     * initialize a named session
     *
     * @param string $sessionname session name (optional)
     *
     * @return void
     */
    public function sessionInit($sessionname = 'PHPSESSION')
    {
        if ($sessionname)
            $this->SESSIONNAME = $sessionname;
        session_name($this->SESSIONNAME);
        $sid = preg_replace('=\?.*$=', '', sendVar(session_name()));
        if ($sid)
            session_id($sid);
        // set path for session data
        $this->SESSIONPATH = WRITEABLEDIR . 'sessiondata';
        // check of existance
        if (!is_dir($this->SESSIONPATH))
            if (!createDir($this->SESSIONPATH))
                trigger_error("Can not create session directory\n" . $this->SESSIONPATH, E_USER_ERROR);
        session_save_path($this->SESSIONPATH);
        session_start();
        // set session lifetime
        ini_set(
            'session.gc_maxlifetime', ($this->session_cookielifetime ?
            ($this->session_cookielifetime * 60) : ini_get('session.gc_maxlifetime'))
        );
        $time = (
            ($this->session_cookielifetime) ?
            ($this->session_cookielifetime * 60 ) : ini_get('session.gc_maxlifetime')
        );
        $time = $time + time();
        // setcookie(session_name(), session_id(), ($time>time() ? $time : "0"), '/');
        // setcookie(session_name(), session_id(), '0', '/');
        return;
    }
    /**
     * constructor init session
     *
     * @return void
     */
    protected function __construct()
    {
        $this->sessionInit();
    }
    /**
     * return a instance of object
     *
     * @param string $sessionname session name (optional)
     *
     * @return object instance
     */
    public static function getInstance($sessionname = 'PHPSESSION')
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c($sessionname);
        }
        return self::$INSTANCE;
    }
    /**
     * register a variable with the value in the session
     *
     * @param string $var var name
     * @param mixed  $val value
     *
     * @return bool
     */
    public function set($var, $val = null)
    {
        return $_SESSION[$var] = $val;
    }
    /**
     * unregister a variable
     *
     * @param string $var var name
     *
     * @return string old value of var
     */
    public function delete($var)
    {
        $temp = $this->get($var);
        session_unregister($var);
        return $temp;
    }
    /**
     * returns a registered variable content
     *
     * @param string $var var name
     *
     * @return mixed value
     */
    public function get($var)
    {
        if (!isset ($_SESSION[$var]))
            return false;
        return $_SESSION[$var];
    }
    /**
     * return the session identifier
     *
     * @return string session id
     */
    public function getSessionId()
    {
        return session_id();
    }
    /**
     * return the session name
     *
     * @return string session name
     */
    public function getSessionName()
    {
        return $this->SESSIONNAME;
    }
    /**
     * destroy the session and delete all registered vars
     *
     * @return void
     */
    public function destroy()
    {
        if (isset($_COOKIE[session_name()]))
            setcookie(session_name(), '', time()-42000, '/');
        session_destroy();
    }
}